﻿

#region Using directives
using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Arch;
using InvoiceChecking.Data;
using InvoiceChecking.Data.SqlClient;
using InvoiceChecking.Entities;
using InvoiceChecking.Web.UI;
#endregion

public partial class ManageVoucherPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        FormUtil.SetPageIndex(GridView1, "page");
        FormUtil.SetDefaultButton((Button)GridViewSearchPanel1.FindControl("cmdSearch"));
        //WhereClauseHiddenField.Value = GetWhereClause();
        GridViewSearchPanel1.Filter = @"SunRefer NOT LIKE 'CCA%' AND SunRefer IS NOT NULL AND JRNAL_SRCE='PAYMT'";
    }

    protected void btnApply_Click(object sender, EventArgs e)
    {
        if (ddlAction.SelectedValue == "Print")
        {
            int count = SaveSelectedRow();
            if (count > 0)
            {
                Response.Redirect("ReportExportPayment.aspx", true);
            }
        }
    }


    private int SaveSelectedRow()
    {
        DataSet journal = null;
        DataSet invoiceDetails = null;
        int count = 0;
        foreach (GridViewRow row in GridView1.Rows)
        {
            var cbCheck = (CheckBox)row.FindControl("cb_Check");
            if (cbCheck.Checked)
            {
                count++;
                int batchNo = Convert.ToInt32(row.Cells[4].Text);
                int allocRef = Convert.ToInt32(row.Cells[18].Text);
                var sqlCommand = new SqlCommand
                {
                    CommandText = "_SunInvoice_Invoice",
                    CommandType = CommandType.StoredProcedure
                };
                sqlCommand.Parameters.AddWithValue("JrnalNo", batchNo);
                sqlCommand.Parameters.AddWithValue("AllocRef", allocRef);
                if (journal == null)
                {
                    journal = DataRepository.Provider.ExecuteDataSet(sqlCommand);
                    invoiceDetails = DataRepository.SunInvoiceProvider.InvoiceDetails(Convert.ToInt32(allocRef));
                }
                else
                {
                    DataSet temp = DataRepository.Provider.ExecuteDataSet(sqlCommand);
                    journal.Tables[0].Rows.Add(temp.Tables[0].Rows[0].ItemArray);
                    DataSet tempDetails = DataRepository.SunInvoiceProvider.InvoiceDetails(allocRef);
                    foreach (DataRow tempRow in tempDetails.Tables[0].Rows)
                    {
                        invoiceDetails.Tables[0].Rows.Add(tempRow.ItemArray);
                    }
                }
            }
        }
        Session["VoucherInvoice"] = journal;
        Session["VoucherInvoiceDetails"] = invoiceDetails;
        return count;
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        HttpCookie httpCookie = Request.Cookies["ICConnectionString"];
        if (httpCookie != null)
        {
            SqlNetTiersProvider provider = GlobalUtilities.CreateSunProvider(Server.MapPath("~") + "/Config.xml",
                                                                          httpCookie.Value);
            if (provider != null)
                DataRepository.LoadProvider(provider, true);
        }
    }
}


